package com.architecture.core.utils;

import com.architecture.core.except.ErrException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * <p>Project: BTC</p>
 * <p>Description: </p>
 * <p>Copyright (c) 2012 Wilmar Consultancy Services</p>
 * <p>All Rights Reserved.</p>
 *
 * @author <a href="mailto:hujianguang@wcs-global.com">胡建光</a>
 */
public final class TimeUtils {

    private static final SimpleDateFormat defaultDateFormat = new SimpleDateFormat(ConfigUtil.getString(ConfigUtil.W_DEFAULT_DATE_FORMAT));


    /**
     * 取得按格式输出的时间字符串
     *
     * @param time
     * @return
     * @throws ErrException
     */
    public static final String getTimeString(Timestamp time) throws ErrException {

        if (null == time) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        return defaultDateFormat.format(time);

    }

    /**
     * 输出指定格式的日期字符串,如果日期格式未定义则返回默认日期字符串
     * @param time
     * @param timeFormat
     * @return
     * @throws ErrException
     */
    public static final String getTimeString(Timestamp time, ConfigUtil timeFormat) throws ErrException {

        if (null == time) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        if (null == timeFormat) {
            return getTimeString(time);
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(ConfigUtil.getString(timeFormat));
        return dateFormat.format(time);
    }

}
